using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SIMc
{
    public partial class Chat : Form
    {
        string od;
        string textik;
        bool poslane;
        string uri;
        public MainForm form1;
        public TabPageController pageController;

        /// <summary>
        /// odstrani z retazca <sip: na zaciatku a > na konci
        /// </summary>
        /// <param name="odstran"></param>
        /// <returns></returns>
        public string odstranBlbosti(string odstran){
            odstran=odstran.Remove(0, 5);
            odstran=odstran.Remove(odstran.Length - 1, 1);
            return odstran;
        }


        public void chcemPoslat(string name, string uri){
            pageController.wantToWrite(name, uri);
        
        }
        /// <summary>
        /// z hlavneho formu volana metoda ked pride sprava (cez delegata)
        /// </summary>
        /// <param name="from">od uzivatela, tvar <sip:user@domain></param>
        /// <param name="text"></param>
        public void prislaSprava(string from, string text)
        {
            pageController.insertIntoTab(from, text);
            if (closed == true)
            {
                closed = false;
                this.Show();

            }
        }

        /// <summary>
        /// konstruktor
        /// </summary>
        /// <param name="form1">form ktoremu Chat patri</param>
        public Chat(MainForm form1)
        {
            InitializeComponent();
            pageController = new TabPageController(form1.xmlContact, tabControl1, form1.profil, form1.xmlHistory);
            pageController.keybEnabled = this.inputPanel1.Enabled;
            pageController.keybHeight = this.inputPanel1.Bounds.Height;
            poslane = false;
            closed = true;
            this.form1 = form1;
            //InitializeComponent();
            SuspendLayout();

            //TB_chat.AcceptsReturn = true;
            //TB_chat.AcceptsTab = true;
            panel1.Height = inputPanel1.VisibleDesktop.Height - 30;
            tabControl1.Height = inputPanel1.VisibleDesktop.Height - 30;
            panel1.Width = inputPanel1.VisibleDesktop.Width;
            tabControl1.Width = inputPanel1.VisibleDesktop.Width;
            ResumeLayout();
        }

        /// <summary>
        /// posunutie a zmena velkosti komponent ked zapnem klavesnicu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            if (inputPanel1.Enabled)
            {
                panel1.Height = panel1.Height-inputPanel1.Bounds.Height-3;
                tabControl1.Height = tabControl1.Height- inputPanel1.Bounds.Height-3;
                pageController.inputPanelEnabled(inputPanel1.Bounds.Height);

            }
            else {
                panel1.Height = inputPanel1.VisibleDesktop.Height-30;
                tabControl1.Height = inputPanel1.VisibleDesktop.Height-30;
                pageController.inputPanelDisabled(inputPanel1.Bounds.Height);
            }
        }

        /// <summary>
        /// klik na poslanie spravy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void posliSpravu(string uri, string text){
            this.form1.sendMessage(uri, text);
        }

        private void B_send_Click(object sender, EventArgs e)
        {

            if (TF_send.Text.Equals("")) {
                MessageBox.Show("Prosim neposielajte prazdne spravy");
                return;
            }

            //TB_chat.Text = TB_chat.Text + "JA" + "\r\n";
            //TB_chat.Text = TB_chat.Text + TF_send.Text + "\r\n";
            //TB_chat.SelectionStart = TB_chat.Text.Length;
            //TB_chat.ScrollToCaret();
            

            this.form1.sendMessage(this.uri, TF_send.Text.ToString());
            TF_send.Text = "";
        }

        /// <summary>
        /// premenna indikujuca ci je chatform otvoreny alebo zatvoreny
        /// </summary>
        public bool closed;

        /// <summary>
        /// pri zavreti okna -> nastavenie premennej, aby som ho pri dalsej sprave otvoril (z hlavneho formu)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Chat_Closed(object sender, EventArgs e)
        {
            closed = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}